package data;

/**
 * @author Tang Jiujia
 * @since 9:44 2020-04-08
 */
public class StringArrayQueue {
    /**
    *  队列的大小
    * */
    private int n;
    /**
    * 队列头的索引位置
    * */
    private int head = 0;
    /**
    * 队列尾的索引位置
    * */
    private int tail = 0;
    /**
    * 队列数组
    * */
    private String[] array;

    public StringArrayQueue(int capacity) {
        n = capacity;
        array = new String[n];
    }

    /**
     * 入队操作
     *
     * @param element 入队元素
     * @return 入队成功返回true，失败返回false
     */
    public boolean enqueue(String element) {
        if (tail == n) {
            if (head == 0) {
                return false;
            }
            //数据迁移
            int len = tail - head;
            for (int i = 0; i < len; i++) {
                array[i] = array[head];
                ++head;
            }
            //重置头、尾指针
            tail = len + 1;
            head = 0;
        }
        array[tail] = element;
        ++tail;
        return true;
    }

    /**
     * 出队操作
     *
     * @param
     * @return java.lang.String
     */
    public String dequeue() {
        if (head == tail) {
            return null;
        }
        String result = array[head];
        array[head] = null;
        ++head;
        return result;
    }
}
































